﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GAMS.DAL;
using KPIS.GERP.GAMS.MODEL;
using System.Collections.Generic;

namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ACM_INVENTORY_Control : System.Web.UI.Page
    {
        Commons.Main main = new Commons.Main();
        D_ACM_INVENTORY_Control d_acm_inventory = new D_ACM_INVENTORY_Control();
        D_ACM_MAS_Account d_acm_mas_account = new D_ACM_MAS_Account();
        M_ACM_INVENTORY_Control m_acm_inventory = new M_ACM_INVENTORY_Control();
        M_ACM_MAS_Account info_asset;
        M_ACM_MAS_Account info_consume;
        M_ACM_MAS_Account info_cumdepre;
        M_ACM_MAS_Account info_depre;
        M_ACM_MAS_Account info_break;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 UpdateUI1("Load");
                 LoadListBox();
                 OpenWin_PanelInventory();
                 OpenWin_PanelAsset();
                 OpenWin_PanelLocality();
                 OpenWin_PanelConstruct();
                 grdInventory.DataSource = "";
                 grdInventory.DataBind();
                //LoadDataToInventoryCombo();
                //LoadDataToAssetCombo();
                //LoadDataToLocalityCombo();
                //LoadDataToConstructCombo();
                 EnablePanel(false);
            }

        }
        protected void cmbGroupType_OnSelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            string t = "test";
        }
        protected void OpenWin_PanelInventory()
        {
            btnaccountasset_code_Inventory.OnClientClick = "openWin('cmbaccountasset_code_Inventory')";
            btnaccount_consume_Inventory.OnClientClick = "openWin('cmbaccount_consume_Inventory')";
        }
        protected void OpenWin_PanelAsset()
        {           
            btnaccountasset_code_asset.OnClientClick = "openWin('cmbaccountasset_code_asset')";
            btnaccountcumdepre_code_asset.OnClientClick = "openWin('cmbaccountcumdepre_code_asset')";
            btnaccountdepre_code_asset.OnClientClick = "openWin('cmbaccountdepre_code_asset')";
            btnaccountbreak_code_asset.OnClientClick = "openWin('cmbaccountbreak_code_asset')";
        }
        protected void OpenWin_PanelLocality()
        {
            btnaccountasset_code_locality.OnClientClick = "openWin('cmbaccountasset_code_locality')";
        }
        protected void OpenWin_PanelConstruct()
        {
            btnaccountasset_code_construct.OnClientClick = "openWin('cmbaccountasset_code_construct')";
            btnaccountcumdepre_code_construct.OnClientClick ="openWin('cmbaccountcumdepre_code_construct')";
            btnaccountdepre_code_construct.OnClientClick = "openWin('cmbaccountdepre_code_construct')";
            btnaccountbreak_code_construct.OnClientClick = "openWin('cmbaccountbreak_code_construct')";
        }
        protected void LoadListBox()
        {
            RadComboBoxItem item0 = new RadComboBoxItem("กรุณาเลือกรายการ", "");
            cmbInventoryType.Items.Add(item0);
            RadComboBoxItem item1 = new RadComboBoxItem("วัสดุ", "1");
            cmbInventoryType.Items.Add(item1);
            RadComboBoxItem item2 = new RadComboBoxItem("ครุภัณฑ์", "2");
            cmbInventoryType.Items.Add(item2);
            RadComboBoxItem item3 = new RadComboBoxItem("ที่ดิน", "3");
            cmbInventoryType.Items.Add(item3);
            RadComboBoxItem item4 = new RadComboBoxItem("สิ่งก่อสร้าง", "4");
            cmbInventoryType.Items.Add(item4);
        }

        protected void cmbaccountasset_code_Inventory_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbaccountasset_code_Inventory.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbaccount_consume_Inventory_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbaccount_consume_Inventory.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbaccountasset_code_asset_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbaccountasset_code_asset.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbaccountcumdepre_code_asset_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbaccountcumdepre_code_asset.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbaccountdepre_code_asset_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbaccountdepre_code_asset.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbaccountbreak_code_asset_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbaccountbreak_code_asset.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbaccountasset_code_locality_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbaccountasset_code_locality.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }

        protected void cmbaccountasset_code_construct_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbaccountasset_code_construct.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbaccountcumdepre_code_construct_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbaccountcumdepre_code_construct.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbaccountdepre_code_construct_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbaccountdepre_code_construct.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbaccountbreak_code_construct_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbaccountbreak_code_construct.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }
        //protected void LoadDataToInventoryCombo()
        //{
        //    IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListAccount();
            
        //    //cmbaccountasset_code_Inventory.DataSource = infoList;
        //    //cmbaccountasset_code_Inventory.DataBind();
        //    //main.InsertEmptyDDLSelect(cmbaccountasset_code_Inventory);

        //    //cmbaccount_consume_Inventory.DataSource = infoList;
        //    //cmbaccount_consume_Inventory.DataBind();
        //    //main.InsertEmptyDDLSelect(cmbaccount_consume_Inventory);

        //    //cmbaccountasset_code_asset.DataSource = infoList;
        //    //cmbaccountasset_code_asset.DataBind();
        //    //main.InsertEmptyDDLSelect(cmbaccountasset_code_asset);

        //    //cmbaccountcumdepre_code_asset.DataSource = infoList;
        //    //cmbaccountcumdepre_code_asset.DataBind();
        //    //main.InsertEmptyDDLSelect(cmbaccountcumdepre_code_asset);

        //    //cmbaccountdepre_code_asset.DataSource = infoList;
        //    //cmbaccountdepre_code_asset.DataBind();
        //    //main.InsertEmptyDDLSelect(cmbaccountdepre_code_asset);

        //    //cmbaccountbreak_code_asset.DataSource = infoList;
        //    //cmbaccountbreak_code_asset.DataBind();
        //    //main.InsertEmptyDDLSelect(cmbaccountbreak_code_asset);

        //    //cmbaccountasset_code_locality.DataSource = infoList;
        //    //cmbaccountasset_code_locality.DataBind();
        //    //main.InsertEmptyDDLSelect(cmbaccountasset_code_locality);

        //    //cmbaccountasset_code_construct.DataSource = infoList;
        //    //cmbaccountasset_code_construct.DataBind();
        //    //main.InsertEmptyDDLSelect(cmbaccountasset_code_construct);

        //    //cmbaccountcumdepre_code_construct.DataSource = infoList;
        //    //cmbaccountcumdepre_code_construct.DataBind();
        //    //main.InsertEmptyDDLSelect(cmbaccountcumdepre_code_construct);

        //    //cmbaccountdepre_code_construct.DataSource = infoList;
        //    //cmbaccountdepre_code_construct.DataBind();
        //    //main.InsertEmptyDDLSelect(cmbaccountdepre_code_construct);

        //    //cmbaccountbreak_code_construct.DataSource = infoList;
        //    //cmbaccountbreak_code_construct.DataBind();
        //    //main.InsertEmptyDDLSelect(cmbaccountbreak_code_construct);
        //}

        protected void grdInventory_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "DataEdit")
            {
                EnableObj(true);
                UpdateUI1("Update");
                M_ACM_INVENTORY_Control info = d_acm_inventory.GetListFillBySeq(main.ConvertNullInt(grdInventory.Items[e.Item.ItemIndex]["resource_seq"].Text));
                lblpInventoryCode.Text = info.resource_code.ToString();
                lblpInventoryName.Text = info.resource_name.ToString();
                hresourceseq.Text = info.resource_seq.ToString();

                if (cmbInventoryType.Text == "วัสดุ")
                {
                    info_asset = d_acm_mas_account.GetListFillByAccountSeq(info.AssetAccountId);
                    cmbaccountasset_code_Inventory.Text = info_asset.account_code_name;
                    cmbaccountasset_code_Inventory.SelectedValue = info.AssetAccountId.ToString();
               
                    info_consume = d_acm_mas_account.GetListFillByAccountSeq(info.ConsumeAccountId);
                    cmbaccount_consume_Inventory.Text = info_consume.account_code_name;
                    cmbaccount_consume_Inventory.SelectedValue = info.ConsumeAccountId.ToString();
                }
                else if (cmbInventoryType.Text == "ครุภัณฑ์")
                {
                    info_asset = d_acm_mas_account.GetListFillByAccountSeq(info.AssetAccountId);
                    cmbaccountasset_code_asset.Text = info_asset.account_code_name;
                    cmbaccountasset_code_asset.SelectedValue = info.AssetAccountId.ToString();

                    info_cumdepre = d_acm_mas_account.GetListFillByAccountSeq(info.CumdepreAccountId);
                    cmbaccountcumdepre_code_asset.Text = info_cumdepre.account_code_name;
                    cmbaccountcumdepre_code_asset.SelectedValue = info.CumdepreAccountId.ToString();

                    info_depre = d_acm_mas_account.GetListFillByAccountSeq(info.DepreAccountId);
                    cmbaccountdepre_code_asset.Text = info_depre.account_code_name;
                    cmbaccountdepre_code_asset.SelectedValue = info.DepreAccountId.ToString();

                    info_break = d_acm_mas_account.GetListFillByAccountSeq(info.BreakAccountId);
                    cmbaccountbreak_code_asset.Text = info_break.account_code_name;
                    cmbaccountbreak_code_asset.SelectedValue = info.BreakAccountId.ToString();

                   // cmbaccountasset_code_asset.SelectedValue = info.AssetAccountId.ToString();
                   // cmbaccountcumdepre_code_asset.SelectedValue = info.CumdepreAccountId.ToString();
                   // cmbaccountdepre_code_asset.SelectedValue = info.DepreAccountId.ToString();
                   // cmbaccountbreak_code_asset.SelectedValue = info.BreakAccountId.ToString();
                }
                else if (cmbInventoryType.Text == "ที่ดิน")
                {
                    info_asset = d_acm_mas_account.GetListFillByAccountSeq(info.AssetAccountId);
                    cmbaccountasset_code_locality.Text = info_asset.account_code_name;
                    cmbaccountasset_code_locality.SelectedValue = info.AssetAccountId.ToString();
                    //cmbaccountasset_code_locality.SelectedValue = info.AssetAccountId.ToString();
                }
                else if (cmbInventoryType.Text == "สิ่งก่อสร้าง")
                {
                    info_asset = d_acm_mas_account.GetListFillByAccountSeq(info.AssetAccountId);
                    cmbaccountasset_code_construct.Text = info_asset.account_code_name;
                    cmbaccountasset_code_construct.SelectedValue = info.AssetAccountId.ToString();

                    info_cumdepre = d_acm_mas_account.GetListFillByAccountSeq(info.CumdepreAccountId);
                    cmbaccountcumdepre_code_construct.Text = info_cumdepre.account_code_name;
                    cmbaccountcumdepre_code_construct.SelectedValue = info.CumdepreAccountId.ToString();

                    info_depre = d_acm_mas_account.GetListFillByAccountSeq(info.DepreAccountId);
                    cmbaccountdepre_code_construct.Text = info_depre.account_code_name;
                    cmbaccountdepre_code_construct.SelectedValue = info.DepreAccountId.ToString();

                    info_break = d_acm_mas_account.GetListFillByAccountSeq(info.BreakAccountId);
                    cmbaccountbreak_code_construct.Text = info_break.account_code_name;
                    cmbaccountbreak_code_construct.SelectedValue = info.BreakAccountId.ToString();
                    //cmbaccountasset_code_construct.SelectedValue = info.AssetAccountId.ToString();
                    //cmbaccountcumdepre_code_construct.SelectedValue = info.CumdepreAccountId.ToString();
                    //cmbaccountdepre_code_construct.SelectedValue = info.DepreAccountId.ToString();
                    //cmbaccountbreak_code_construct.SelectedValue = info.BreakAccountId.ToString();
                }
            }
        }
        
        protected void cmbInventoryType_OnSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {                                                     
            ClearObj();
            UpdateUI1("Combo");
            
            if (cmbInventoryType.Text == "วัสดุ")
            {
                panelInventory.Visible = true;
                panelAsset.Visible = false;
                panelLocality.Visible = false;
                panelConstruct.Visible = false;
               // grdInventory.Columns.FindByUniqueName("asset_account").Visible = false;
                grdInventory.Columns.FindByUniqueName("cumdepre_account").Visible = false;
                grdInventory.Columns.FindByUniqueName("depre_account").Visible = false;
                grdInventory.Columns.FindByUniqueName("consume_account").Visible = true;
                grdInventory.Columns.FindByUniqueName("break_account").Visible = false;
                //grdInventory.Columns.FindByUniqueName("cumdepre_account").HeaderText = "";
                grdInventory.DataSource = d_acm_inventory.GetListFillByType(1);
                grdInventory.DataBind();
            }
            else if (cmbInventoryType.Text == "ครุภัณฑ์")
            {
                panelInventory.Visible = false;
                panelAsset.Visible = true;
                panelLocality.Visible = false;
                panelConstruct.Visible = false;
                grdInventory.Columns.FindByUniqueName("cumdepre_account").Visible = true;
                grdInventory.Columns.FindByUniqueName("depre_account").Visible = true;
                grdInventory.Columns.FindByUniqueName("consume_account").Visible = false;
                grdInventory.Columns.FindByUniqueName("break_account").Visible = true;
                grdInventory.Columns.FindByUniqueName("break_account").HeaderText = "รหัสบัญชีพักพัสดุ";
                grdInventory.DataSource = d_acm_inventory.GetListFillByType(2) ;
                grdInventory.DataBind();
            }
            else if (cmbInventoryType.Text == "ที่ดิน")
            {
                panelInventory.Visible = false;
                panelAsset.Visible = false;
                panelLocality.Visible = true;
                panelConstruct.Visible = false;
                grdInventory.Columns.FindByUniqueName("cumdepre_account").Visible = false;
                grdInventory.Columns.FindByUniqueName("depre_account").Visible = false;
                grdInventory.Columns.FindByUniqueName("consume_account").Visible = false;
                grdInventory.Columns.FindByUniqueName("break_account").Visible = false;
                grdInventory.DataSource = d_acm_inventory.GetListFillByType(3);
                grdInventory.DataBind();
            }
            else if (cmbInventoryType.Text == "สิ่งก่อสร้าง")
            {
                panelInventory.Visible = false;
                panelAsset.Visible = false;
                panelLocality.Visible = false;
                panelConstruct.Visible = true;
                grdInventory.Columns.FindByUniqueName("cumdepre_account").Visible = true;
                grdInventory.Columns.FindByUniqueName("depre_account").Visible = true;
                grdInventory.Columns.FindByUniqueName("consume_account").Visible = false;
                grdInventory.Columns.FindByUniqueName("break_account").Visible = true;
                grdInventory.Columns.FindByUniqueName("break_account").HeaderText = "รหัสบัญชีพักสิ่งก่อสร้าง";
                grdInventory.DataSource = d_acm_inventory.GetListFillByType(4);
                grdInventory.DataBind();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateCheck())
                {
                    EnableObj(false);
                    UpdateUI1("Save");
                    m_acm_inventory.resource_seq = Convert.ToInt32(hresourceseq.Text);
                    if (cmbInventoryType.Text == "วัสดุ")
                    {
                        m_acm_inventory.AssetAccountId = main.ConvertNullInt(cmbaccountasset_code_Inventory.SelectedValue);
                        m_acm_inventory.ConsumeAccountId = main.ConvertNullInt(cmbaccount_consume_Inventory.SelectedValue);
                    }
                    else if (cmbInventoryType.Text == "ครุภัณฑ์")
                    {
                        m_acm_inventory.AssetAccountId = main.ConvertNullInt(cmbaccountasset_code_asset.SelectedValue);
                        m_acm_inventory.CumdepreAccountId = main.ConvertNullInt(cmbaccountcumdepre_code_asset.SelectedValue);
                        m_acm_inventory.DepreAccountId = main.ConvertNullInt(cmbaccountdepre_code_asset.SelectedValue);
                        m_acm_inventory.BreakAccountId = main.ConvertNullInt(cmbaccountbreak_code_asset.SelectedValue);
                    }
                    else if (cmbInventoryType.Text == "ที่ดิน")
                    {
                        m_acm_inventory.AssetAccountId = main.ConvertNullInt(cmbaccountasset_code_locality.SelectedValue);
                    }
                    else if (cmbInventoryType.Text == "สิ่งก่อสร้าง")
                    {
                        m_acm_inventory.AssetAccountId = main.ConvertNullInt(cmbaccountasset_code_construct.SelectedValue);
                        m_acm_inventory.CumdepreAccountId = main.ConvertNullInt(cmbaccountcumdepre_code_construct.SelectedValue);
                        m_acm_inventory.DepreAccountId = main.ConvertNullInt(cmbaccountdepre_code_construct.SelectedValue);
                        m_acm_inventory.BreakAccountId = main.ConvertNullInt(cmbaccountbreak_code_construct.SelectedValue);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                d_acm_inventory.UPDATE(m_acm_inventory);
                LoadGrid();
                grdInventory.DataBind();
            }
        }
        protected void LoadGrid()
        {
            
            if (cmbInventoryType.Text == "วัสดุ")
            {
                grdInventory.DataSource = d_acm_inventory.GetListFillByType(1);
                //grdInventory.DataBind();
            }
            else if (cmbInventoryType.Text == "ครุภัณฑ์")
            {
                grdInventory.DataSource = d_acm_inventory.GetListFillByType(2);
               // grdInventory.DataBind();
            }
            else if (cmbInventoryType.Text == "ที่ดิน")
            {
                grdInventory.DataSource = d_acm_inventory.GetListFillByType(3);
                //grdInventory.DataBind();
            }
            else if (cmbInventoryType.Text == "สิ่งก่อสร้าง")
            {
                grdInventory.DataSource = d_acm_inventory.GetListFillByType(4);
               // grdInventory.DataBind();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            EnableObj(false);
            UpdateUI1("Cancel");
        }
        protected void grdInventory_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
               // grdInventory.DataSource = d_acm_inventory.GetList(); 
                LoadGrid();
            }
        }
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            string strArg;
            strArg = e.Argument;
            string strSeq = strArg.Split(Convert.ToChar("|"))[0];
            string strControlName = strArg.Split(Convert.ToChar("|"))[1];//strArg.Split(Convert.ToChar("|"))[1]; Ji
            ((RadComboBox)PanelData.FindControl(strControlName)).SelectedValue = strSeq;
            M_ACM_MAS_Account infoacc = d_acm_mas_account.GetListFillByAccountSeq(Convert.ToInt32(strSeq));
            ((RadComboBox)PanelData.FindControl(strControlName)).Text = infoacc.account_code_name;
        }
        //============================ control events ==============================
        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "Load":
                    lblStatus.Text = "แสดงสถานะการทำงาน";
                    main.StrSelectWord = "< เลือกรายการรหัสบัญชี >";
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    ClearObj();
                    EnableObj(false);
                    break;
                case "Update":
                    lblStatus.Text = "แก้ไขข้อมูลจากที่เลือก";
                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    EnableObj(true);
                    break;
                case "Save":
                    lblStatus.Text = "บันทึกข้อมูลเรียบร้อยแล้ว";
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    EnableObj(false);
                    break;
                case "Cancel":
                    lblStatus.Text = "ยกเลิกข้อมูล";
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    ClearObj();
                    EnableObj(false);
                    break;
                case "Combo":
                    lblStatus.Text = "รายการประเภทพัสดุ";
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    EnableObj(false);
                    break;
            }
        }
        private void ClearObj()
        {
            lblpInventoryCode.Text = ".....";
            lblpInventoryName.Text = ".....";
            
            //Inventory
            cmbaccountasset_code_Inventory.SelectedValue = "";
            cmbaccount_consume_Inventory.SelectedValue = "";
            //Asset
            cmbaccountasset_code_asset.SelectedValue = "";
            cmbaccountcumdepre_code_asset.SelectedValue = "";
            cmbaccountdepre_code_asset.SelectedValue = "";
            cmbaccountbreak_code_asset.SelectedValue = "";
            //Locality
            cmbaccountasset_code_locality.SelectedValue = "";
            //Construct
            cmbaccountasset_code_construct.SelectedValue = "";
            cmbaccountcumdepre_code_construct.SelectedValue="";
            cmbaccountdepre_code_construct.SelectedValue = "";
            cmbaccountbreak_code_construct.SelectedValue="";

        }
        protected void EnableObj(Boolean pFlag)
        {
            //Inventory
            cmbaccountasset_code_Inventory.Enabled = pFlag;
            cmbaccount_consume_Inventory.Enabled = pFlag;
            btnaccountasset_code_Inventory.Enabled = pFlag;
            btnaccount_consume_Inventory.Enabled = pFlag;
            //Asset
            cmbaccountasset_code_asset.Enabled = pFlag;
            cmbaccountcumdepre_code_asset.Enabled = pFlag;
            cmbaccountdepre_code_asset.Enabled = pFlag;
            cmbaccountbreak_code_asset.Enabled = pFlag;

            btnaccountasset_code_asset.Enabled = pFlag;
            btnaccountcumdepre_code_asset.Enabled = pFlag;
            btnaccountdepre_code_asset.Enabled = pFlag;
            btnaccountbreak_code_asset.Enabled = pFlag;
            //Locality
            cmbaccountasset_code_locality.Enabled = pFlag;
            btnaccountasset_code_locality.Enabled = pFlag;
            //Construct
            cmbaccountasset_code_construct.Enabled = pFlag;
            cmbaccountcumdepre_code_construct.Enabled = pFlag;
            cmbaccountdepre_code_construct.Enabled = pFlag;
            cmbaccountbreak_code_construct.Enabled = pFlag;

            btnaccountasset_code_construct.Enabled = pFlag;
            btnaccountcumdepre_code_construct.Enabled = pFlag;
            btnaccountdepre_code_construct.Enabled = pFlag;
            btnaccountbreak_code_construct.Enabled = pFlag;

        }
        protected void EnablePanel(Boolean pFlag)
        {
            panelInventory.Visible = pFlag;
            panelAsset.Visible = pFlag;
            panelLocality.Visible = pFlag;
            panelConstruct.Visible = pFlag;
        }
        protected Boolean ValidateCheck()
        {
            Boolean pFlag  = true;

            if ((hresourceseq.Text == "") || (hresourceseq.Text.Length < 1))
            {
                lblStatus.Text = "กรุณาตรวจสอบการเรียกข้อมูลใหม่";
                pFlag = false;
            }
            return pFlag;
        }

    }
}
